package com.qingfeng.base.service;

import com.qingfeng.base.dao.TUserRoleMapper;
import com.qingfeng.base.model.TRole;
import com.qingfeng.client.bean.Seller;
import com.qingfeng.client.dao.SellerMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author zhangyuefei
 * @version 1.0
 * @date 2019/9/18 5:19 下午
 */
@Slf4j
@Service
public class TUserService {

    @Autowired
    private TUserRoleMapper userRoleMapper;

    @Autowired
    private SellerMapper userMapper;

    @Transactional
    public String save(Seller user) {
        if (!checkName(user)){
            return "用户名重复";
        }
        if (!checkPhone(user)){
            return "手机号重复";
        }
//        if (!checkAuthAccount(user)){
//            return "证件号重复";
//        }
        int index = userMapper.insert(user);
        List<TRole> roles = user.getRoles();
        index = userRoleMapper.insertList(user.getId(),roles);
        if (index<0){
            return "数据插入失败，请重试";
        }
        return null;
    }

    @Transactional
    public String update(Seller user) {
        user.setStoreId(null);
        Seller seller = userMapper.selectById(user.getId());
        if (!checkName(user)){
            return "用户名重复";
        }
        if (!checkPhone(user)){
            return "手机号重复";
        }

        int index = userMapper.updateById(user);
        userRoleMapper.deleteByUserId(user.getId());
        List<TRole> roles = user.getRoles();
        index = userRoleMapper.insertList(user.getId(),roles);
        if (index<0){
            return "数据更新失败，请重试";
        }
        return null;
    }


    public int deletes(List<Long> idList) {

        int index = userMapper.deleteBatchIds(idList);
        index = userRoleMapper.deleteByUserIds(idList);
        return index;
    }


    private boolean checkName(Seller user){
        String name = user.getName();
        if (StringUtils.isEmpty(name)) return false;
        List<Seller> list = userMapper.selectByName(name);
        if (user.getId()!=null && list.size()==1){
            Seller data = userMapper.selectById(user.getId());
            if (data.getName().equals(name)){
                return true;
            }else {
                return false;
            }
        }
        if (list.size()>0){
            return false;
        }
        return true;
    }

    private boolean checkPhone(Seller user){
        String phone =user.getPhone();
        if (StringUtils.isEmpty(phone)) return false;
        List<Seller> list =  userMapper.selectByPhone(phone);
        if (user.getId()!=null && list.size()==1){
            Seller data = userMapper.selectById(user.getId());
            if (data.getPhone().equals(phone)){
                return true;
            }else {
                return false;
            }
        }
        if (list.size()>0){
            return false;
        }
        return true;
    }

    private boolean checkAuthAccount(Seller user){
        String accout = user.getAuthAccount();
        Integer authType = user.getAuthType();
        if (authType==null) return false;
        if (StringUtils.isEmpty(accout)) return false;
        List<Seller> list = userMapper.selectByAuthAccount(accout);
        if (user.getId()!=null && list.size()==1){
            Seller data = userMapper.selectById(user.getId());
            if (data.getAuthAccount().equals(accout)){
                return true;
            }else {
                return false;
            }
        }
        if (list.size()>0){
            return false;
        }
        return true;
    }
}
